import 'package:flutter/material.dart';
import 'package:tender_flutter/constant/resource_path.dart';
import 'package:tender_flutter/constant/theme_colors.dart';
import 'package:tender_flutter/router/router.dart';
import 'package:tender_flutter/router/router_map.gr.dart';
import 'package:tender_flutter/style/font_style.dart';
import 'package:tender_flutter/utils/screen_adaper.dart';
import 'package:tender_flutter/utils/toast.dart';
import 'package:tender_flutter/widgets/base_appbar.dart';

class CreditSearchPage extends StatefulWidget {
  @override
  _CreditSearchPageState createState() => _CreditSearchPageState();
}

class _CreditSearchPageState extends State<CreditSearchPage>
    with TickerProviderStateMixin {
  TextEditingController _nameController = TextEditingController();
  TextEditingController _codeController = TextEditingController();

  _prepareQuery() {
    if (_nameController.text.isEmpty) {
      ToastUtils.show("请输入查询名称");
      return;
    }
    if (_codeController.text.isEmpty) {
      ToastUtils.show("请输入查询编码");
      return;
    }
    FocusScope.of(context).requestFocus(FocusNode());
    LRouter.navigatorCtx(context).push(Routes.creditListPage,
        arguments: CreditListPageArguments(
          name: _nameController.text,
          code: _codeController.text,
        ));
  }

  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: ThemeColors.colorWhite,
        appBar: _initAppBar(),
        body: _initBody());
  }

  /// 导航
  Widget _initAppBar() {
    return BaseAppBar(context, Text("失信查询", style: FonStyle.navTextStyle()));
  }

  Widget _initBody() {
    return Container(
        padding: EdgeInsets.symmetric(horizontal: ScreenAdapter.width(50)),
        child: Column(children: [_initInputView(), _initSubmitView()]));
  }

  /// 输入体
  Widget _initInputView() {
    return Container(
        margin: EdgeInsets.only(top: ScreenAdapter.height(60)),
        child: Column(children: [
          _getInputItem("icon_name.png", "请输入姓名/企业名称", _nameController),
          _getInputItem("icon_id_number.png", "请输入身份证号/机构代码", _codeController),
        ]));
  }

  /// 提交按钮
  Widget _initSubmitView() {
    return GestureDetector(
        onTap: () {
          _prepareQuery();
        },
        child: Container(
            height: ScreenAdapter.height(90),
            margin: EdgeInsets.only(top: ScreenAdapter.height(60)),
            alignment: Alignment.center,
            decoration: BoxDecoration(
                color: ThemeColors.colorType6,
                borderRadius: BorderRadius.circular(ScreenAdapter.width(10))),
            child: Text(
              "查询",
              style: TextStyle(
                  color: ThemeColors.colorWhite,
                  fontSize: ScreenAdapter.sp(30),
                  fontWeight: FontWeight.w500),
            )));
  }

  Widget _getInputItem(
      String iconName, String placeholder, TextEditingController controller) {
    return Container(
        margin: EdgeInsets.symmetric(vertical: ScreenAdapter.height(10)),
        height: ScreenAdapter.height(90),
        decoration: BoxDecoration(
            color: ThemeColors.colorWhite,
            border: Border.all(color: ThemeColors.colorType45, width: 0.5),
            borderRadius: BorderRadius.circular(ScreenAdapter.width(15))),
        child: Row(children: [
          Container(
              margin: EdgeInsets.symmetric(horizontal: ScreenAdapter.width(30)),
              child: Image.asset(
                RPath.imgPath + iconName,
                fit: BoxFit.scaleDown,
              )),
          Expanded(
              child: TextField(
            decoration: InputDecoration(
              hintText: placeholder,
              focusedBorder: OutlineInputBorder(
                  borderSide: BorderSide(width: 0, color: Colors.transparent)),
              disabledBorder: OutlineInputBorder(
                  borderSide: BorderSide(width: 0, color: Colors.transparent)),
              enabledBorder: OutlineInputBorder(
                  borderSide: BorderSide(width: 0, color: Colors.transparent)),
              border: OutlineInputBorder(
                  borderSide: BorderSide(width: 0, color: Colors.transparent)),
              contentPadding: EdgeInsets.symmetric(vertical: 0),
            ),
            style: TextStyle(
                color: ThemeColors.colorType4, fontSize: ScreenAdapter.sp(32)),
            // keyboardType:
            textAlign: TextAlign.left,
            maxLines: 1,
            autofocus: false,
            controller: controller,
          ))
        ]));
  }
}
